package com.zadtek.sharkRider.activity.adapter;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;

import com.zadtek.sharkRider.activity.common.Const;
import com.zadtek.sharkRider.activity.data.NewVersionData;
import com.zadtek.sharkRider.activity.data.QiangDanData;
import com.zadtek.sharkRider.activity.data.UserData;
import com.zadtek.sharkRider.frame.JsonProcessHelper;

/**
 * 获取MainActivity 的
 * 
 * @author Administrator
 * 
 */
public class MyMainAsyncTask extends AsyncTask<String, String, String> {

	private QiangDanData countdata;
	private UserData qsdata; // 骑士信息
	private QiangDanData setopen; // 设置开收工
	private NewVersionData data;	//新版本信息

	private Context context;
	private Handler mHandler;

	private SharedPreferences sp;

	public MyMainAsyncTask(Context context, Handler mHandler) {
		this.context = context;
		this.mHandler = mHandler;
	}

	@Override
	protected String doInBackground(String... params) {
		// TODO Auto-generated method stub
		String res = "";
		if (params[0].equals("getsum")) {
			sp = context.getSharedPreferences(Const.PREF_NAME,
					Context.MODE_PRIVATE);
			String uid = sp.getString("uid", "");
			countdata = JsonProcessHelper.jsonProcess_getOrderCount(
					"get_order_count", uid);
			if (null != countdata) {
				res = "get_ok";
			} else {
				res = "ping_me_error";
			}

		} else if (params[0].equals("getqsmsg")) { // 获取快递信息
			sp = context.getSharedPreferences(Const.PREF_NAME,
					Context.MODE_PRIVATE);
			String uid = sp.getString("uid", "");
			qsdata = JsonProcessHelper.jsonProcess_getQsinfo("get_emp_info",
					uid);
			if (qsdata != null) {
				res = "qs_ok";
			} else {
				res = "ping_me_error";
			}
		} else if (params[0].equals("setclose")) { // 设置开收工
			sp = context.getSharedPreferences(Const.PREF_NAME,
					Context.MODE_PRIVATE);
			String uid = sp.getString("uid", "");
			setopen = JsonProcessHelper.jsonProcess_setqsopen("set_emp_open",
					uid, params[1]);
			if (setopen != null) {
				res = "setqs_ok";
			} else {
				res = "setqs_fail";
			}
		} else if(params[0].equals("getversion")) {	//获取新版本号
			data = JsonProcessHelper.jsonProcess_getnewverion("version");
			if(data!=null) {
				res = "getversionok";
			} else {
				res = "ping_me_error";
			}
		}

		return res;
	}

	@Override
	protected void onPostExecute(String result) {
		// TODO Auto-generated method stub
		super.onPostExecute(result);
		if (mHandler != null) {
			if (result.equals("get_ok")) {
				Message msg = Message.obtain();
				msg.what = Const.STATE_START_SUCCESS;
				msg.obj = countdata;
				mHandler.sendMessage(msg);
			} else if (result.equals("ping_me_error")) {
				Message msg = Message.obtain();
				msg.what = Const.STATE_START_ERROR;
				mHandler.sendMessage(msg);
			} else if (result.equals("qs_ok")) {
				Message msg = Message.obtain();
				msg.what = Const.STATE_GETNUM_SUCC;
				msg.obj = qsdata;
				mHandler.sendMessage(msg);
			} else if (result.equals("setqs_ok")) {
				Message msg = Message.obtain();
				msg.what = Const.STATE_SETQS_OPEN;
				msg.obj = setopen;
				mHandler.sendMessage(msg);
			} else if (result.equals("setqs_fail")) {
				Message msg = Message.obtain();
				msg.what = Const.STATE_SETQS_FAIL;
				mHandler.sendMessage(msg);
			} else if(result.equals("getversionok")) {
				Message msg = Message.obtain();
				msg.obj = data;
				msg.what = Const.STATE_GETVERSIONOK;
				mHandler.sendMessage(msg);
			}
		}
	}

}
